//
//  BKMyOrderChooseGoEvaluatingVC.m
//  bestkeep
//
//  Created by mac on 16/12/15.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKMyOrderChooseGoEvaluatingVC.h"
#import "BK_OD_Tool.h"
#import "BKMyOrderChooseEvaluatingCell.h"
#import "BKMyOrderGetDataFromNetWork.h"
#import "BK_my_EvaluationOfDryingVC.h"
@interface BKMyOrderChooseGoEvaluatingVC ()
@property(nonatomic,strong)NSMutableArray *dataArr;
@property(nonatomic,strong)NSString *page;

@property(nonatomic,copy)NSString* currentPage;
@property(nonatomic,copy)NSString * total;
@property(nonatomic,strong) ExceptionView *exceptionView;

@end

@implementation BKMyOrderChooseGoEvaluatingVC
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
        
    }return _dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"评价中心";
    self.view.backgroundColor = RGB(242, 242, 242);
  self.currentPage = @"1";
       [self initView];
    [self getDateFromNetwork];
    self.edgesForExtendedLayout = UIRectEdgeNone;

}

-(void)initView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.dataSource = self;
    _tableView.delegate = self;
      _tableView.backgroundColor = ThemeBackgroundColor;
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left);
        make.top.mas_equalTo(self.view.mas_top).offset(0);
        make.right.mas_equalTo(self.view.mas_right);
        make.bottom.mas_equalTo(self.view.mas_bottom);
    }];
    [self setupRefresh];
}
-(void)setupRefresh{
    BKRefreshGifHeader *headerRefresh = [BKRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    self.tableView.mj_header = headerRefresh;
      BKMyOrderFooterAutoReferesh *footer = [BKMyOrderFooterAutoReferesh footerWithRefreshingTarget:self refreshingAction:@selector(updataMore)];
    self.tableView.mj_footer = footer;
    self.tableView.mj_footer.hidden = YES;
    
}
-(void)getDateFromNetwork{
    if (self.exceptionView) {
        [self.exceptionView dismiss];
        self.exceptionView = nil;
    }
    [BKIndicationView showInView:self.view];

    
        [BKMyOrderGetDataFromNetWork  GetOrderEvalutingCount:@"0" OrderId:self.ordId pagesize:@"20" pageno:self.currentPage Success:^(NSArray *OrderArray) {
            [BKIndicationView dismiss];
            [self.dataArr removeAllObjects];
            [self.dataArr addObjectsFromArray:OrderArray];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
       
        [self.tableView reloadData];
            if (self.dataArr.count ==0) {
//                [BKMyOrderExceptionView showInview:self.tableView andStatus:BKMyOrderStatusUnverse title:nil detailedTitle:@"暂无待评价商品" handler:^(BKMyOrderExceptionView *action) {
//                    [self getDateFromNetwork];
//                    [BKMyOrderExceptionView dismiss];
//                }];
                self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeNoData];
                self.exceptionView.contentLabel.text=@"";
                ExceptionViewAction *action = [ExceptionViewAction actionWithType:ExceptionCodeTypeNoData handler:^(ExceptionViewAction *action) {
                    [self.exceptionView dismiss];
                    self.exceptionView = nil;
                    [self getDateFromNetwork];
                }];
                [self.exceptionView addAction:action];
                [self.exceptionView showInView:self.view];

            }
    } failure:^(NSError *error) {
        [BKIndicationView dismiss];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];

        
        ExceptionCodeType errorType;
        if (error.code == -1009) {
            errorType = ExceptionCodeTypeNoNetwork;
        }else if (error.code == -1001){
            errorType = ExceptionCodeTypeRequestFailed;
        }else if (error.code == 502 || error.code == 404){
            errorType = ExceptionCodeTypeServerError;
        }else{
            errorType = ExceptionCodeTypeFailedToLoad;
        }
        
        self.exceptionView = [ExceptionView exceptionViewWithType:errorType];
        ExceptionViewAction *action = [ExceptionViewAction actionWithType:errorType handler:^(ExceptionViewAction *action) {
            [self.exceptionView dismiss];
            self.exceptionView = nil;
            [self getDateFromNetwork];
        }];
        
        [self.exceptionView addAction:action];
        [self.exceptionView showInView:self.view];

        
       }currentPage:^(NSDictionary *Page) {
               self.total  =Page[@"total"];
           if (self.dataArr.count>0&& (self.dataArr.count < [self.total integerValue])) {
               self.tableView.mj_footer.hidden =NO;
           }else{
               self.tableView.mj_footer.hidden = YES;
           }
    }];
}
-(void)refreshData{
    [self.dataArr removeAllObjects];
     self.currentPage = @"1";
    [self getDateFromNetwork];
   }

-(void)updataMore{
    NSInteger page = [self.currentPage integerValue] +1;
    self.currentPage = [NSString stringWithFormat:@"%ld",(long)page];
    [self getDateFromNetwork];
    }


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BKMyOrderChooseEvaluatingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {cell = [[BKMyOrderChooseEvaluatingCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 10000000)];
    if (_dataArr.count) {
        cell.model = _dataArr[indexPath.row];
        cell.evaluteBu.tag = indexPath.row;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.evaluteBu addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
 
    return cell;
}

-(void)buttonAction:(UIButton *)button{
     BKMyOrderEvalutingCountModel *modle = self.dataArr[button.tag];
    BK_my_EvaluationOfDryingVC *vc = [[BK_my_EvaluationOfDryingVC alloc]init];
    vc.order_id = modle.orderId;
    vc.goodsId = modle.goodsId;
    vc.order_item_id = modle.orderItemId;
    vc.img = modle.goodsImg;
    vc.name = modle.goodsName;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
   [self getDateFromNetwork];
}
-(void)viewWillDisappear:(BOOL)animated
{

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
